#include "mainscene.h"
#include "ui_mainscene.h"
#include<QPainter>
#include<mypushbutton.h>
#include<QDebug>
#include<QTimer>
#include<QtMultimedia>
#include"qsoundeffect.h"
MainScene::MainScene(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainScene)
{
    ui->setupUi(this);

    //配置主场景

    //设置固定大小
    setFixedSize(420,688);

    //设置图标
    setWindowIcon(QIcon(":/res/new20240507154444.png"));

    //设置标题
    setWindowTitle("The Main Scene");

    //退出按钮实现
    connect(ui->actionquit,&QAction::triggered,[=](){
        this->close();
    });

    //准备开始按钮的音效
    QSoundEffect*startMusicSound=new QSoundEffect;
    startMusicSound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));

    //开始按钮
    MyPushButton*startMyBtn=new MyPushButton(":/res/MenuSceneStartButton.png");
    startMyBtn->setParent(this);
    startMyBtn->move(this->width()*0.5-startMyBtn->width()*0.5,this->height()*0.7);

    //实例化选择关卡场景
    chooseScene=new ChooseLevelScene;


    //监听选择按钮的返回的信号
    connect(chooseScene,&ChooseLevelScene::SelectSceneBack,this,[=](){
        this->setGeometry(chooseScene->geometry());
        chooseScene->hide();//将选择关卡场景隐藏掉
        this->show();//重新显示主场景
    });

    //监听是否开始效果
    connect(startMyBtn,&MyPushButton::clicked,[=](){
            qDebug()<<"点击了开始";
            //播放开始音效资源
            startMusicSound->play();

            //设置弹起特效
            startMyBtn->zoom1();
            startMyBtn->zoom2();

            //延时进入到选择关卡场景中
            QTimer::singleShot(500,this,[=](){
                this->setGeometry(chooseScene->geometry());
                //自身隐藏
                this->hide();
                //显示选择关卡场景
                chooseScene->show();


            });

        });
}

void MainScene::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/20240507153859.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //画背景上的图标
    pix.load(":/res/20240507154444.png");

    pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);

    painter.drawPixmap(10,30,pix);
}

MainScene::~MainScene()
{
    delete ui;
}
